package com.ctsi.findexpert.service;

import com.ctsi.commons.util.UtilValidate;
import com.ctsi.commons.util.id.UUIDGenerator;
import com.ctsi.commons.util.response.Page;
import com.ctsi.findexpert.dto.AppExpertDTO;
import com.ctsi.findexpert.dto.FindExpertDTO;
import com.ctsi.findexpert.entity.Expert;
import com.ctsi.findexpert.entity.ExpertTags;
import com.ctsi.findexpert.vo.UpdateExpertVO;
import com.ctsi.framework.mybatis.support.MybatisGenericDao;
import com.ctsi.security.ISecurity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class FindExpertCommonService {
    private static final Logger logger = LoggerFactory.getLogger(FindExpertCommonService.class);
    @Autowired
    private MybatisGenericDao dao;

    @Autowired
    private ISecurity iSecurity;

    /**
     * 添加专家
     * @param expert
     * @return
     */
    public int insert(Expert expert, List<String> majorCodes) {
        if (UtilValidate.isEmpty(expert.getId())) {
            expert.setId(UUIDGenerator.getId());
        }
        this.insertTags(expert.getId(),majorCodes);
        logger.debug("insert {}", expert);
        return dao.insert(expert);
    }

    private void insertTags(String expertId,List<String> majorCodes){
        if (UtilValidate.isNotEmpty(majorCodes)) {
            majorCodes.stream().forEach(code -> {
                ExpertTags tag = new ExpertTags();
                tag.setId(UUIDGenerator.getId());
                tag.setExpertId(expertId);
                tag.setMajorCode(code);
                dao.insert(Expert.class.getName() + ".insertTag", tag);
            });
        }
    }

    /**
     * 动态更新
     * @param expert
     * @return
     */
    public int updateByPKSelective(Expert expert,List<String> majorCodes) {
        logger.debug("updateByPrimaryKeySelective {}", expert);
        dao.delete(Expert.class.getName() + ".deleteTag",expert.getId());
        if (UtilValidate.isNotEmpty(majorCodes)) {
            this.insertTags(expert.getId(),majorCodes);
        }
        return dao.update(Expert.class.getName() + ".updateByPrimaryKeySelective", expert);
    }

    /**
     * 更新
     * @param expert
     * @return
     */
    public int update(Expert expert,List<String> majorCodes) {
        logger.debug("update {}", expert);
        dao.delete(Expert.class.getName() + ".deleteTag",expert.getId());
        if (UtilValidate.isNotEmpty(majorCodes)) {
            this.insertTags(expert.getId(),majorCodes);
        }
        return dao.update(expert);
    }

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    public Expert findrById(String id) {
        if (UtilValidate.isEmpty(id)) {
            return null;
        }
        logger.debug("findById {}", id);
        return dao.get(Expert.class, id);
    }

    /**
     * @param id
     * @return
     */
    public UpdateExpertVO findUpdateExpert(String id){
        if (UtilValidate.isEmpty(id)) {
            return null;
        }
        return (UpdateExpertVO) dao.queryForObject(Expert.class.getName()+".findUpdateExpert",id);
    }

    /**
     * 删除Expert对象
     * @param id
     * @return
     */
    public int remove(String id) {
        if (UtilValidate.isNotEmpty(id)) {
            return dao.delete(Expert.class.getName() + ".delete", id);
        }
        dao.delete(Expert.class.getName() + ".deleteTag",id);
        return 0;
    }

    /**
     * 多条件分页查询
     * @param param
     * @param start
     * @param pageSize
     * @return
     */
    public Page<FindExpertDTO> findByMulitCondition(Map<String, Object> param,String userloginId,int start, int pageSize) {
        logger.debug("findByMulitCondition {}", param);
        if (pageSize < 0 || pageSize > 2000)
            pageSize = Page.DEFAULT_PAGE_SIZE;
        if (start != -1) {
            if (!iSecurity.isControl(userloginId,"organChain","SELECT")) {
                return dao.queryPage(Expert.class.getName() + ".selectMulitCondition", param, start, pageSize);
            } else {
                Set<String> dataObjectIds = iSecurity.findDataObjectIds(userloginId,"organChain","SELECT");
                if (UtilValidate.isEmpty(dataObjectIds)) {
                    return new Page<>(0,0,pageSize, Collections.emptyList());
                }
                param.put("dataobjectids",dataObjectIds);
                return dao.queryPage(Expert.class.getName() + ".selectMulitCondition", param, start, pageSize);
            }
        }
        else {
            throw new RuntimeException("参数不正确 !!");
        }
    }

    /**
     * @param param
     * @param start
     * @param pageSize
     * @return
     */
    public Page<AppExpertDTO> findAppExpertDTOByMulitCondition(Map<String, Object> param,int start, int pageSize){
        if (pageSize < 0 || pageSize > 2000) {
            pageSize = Page.DEFAULT_PAGE_SIZE;
        }
        if (start != -1) {
            return dao.queryPage(Expert.class.getName()+".findAppExpertDTO",param,start,pageSize);
        }
        else {
            throw new RuntimeException("参数不正确 !!");
        }
    }

}

